class Solution
{
public:
    int maximumUnits(vector<vector<int>> &boxTypes, int truckSize)
    {

        auto compare = [](auto &l, auto &r)
        {
            return l[1] > r[1];
        };
        sort(boxTypes.begin(), boxTypes.end(), compare);
        int i = 0;
        int n = boxTypes.size();
        int maxUnit = 0;

        while (i < n)
        {
            if (boxTypes[i][0] < truckSize)
            {
                maxUnit += boxTypes[i][0] * boxTypes[i][1];
                truckSize -= boxTypes[i][0];
                ++i;
            }
            else
            {
                maxUnit += boxTypes[i][1] * truckSize;
                break;
            }
        }

        return maxUnit;
    }
};